<template>
  <el-card style="margin:5px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="部门名字">
        <el-input v-model="searchDept.name" placeholder="请输入" style="width:110px;"
                  @keydown.enter="loadDept"></el-input>
      </el-form-item>
      <el-form-item label="部门位置">
        <el-input v-model="searchDept.location" placeholder="请输入" style="width:110px;"
                  @keydown.enter="loadDept"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchDept.status" @change="loadDept" placeholder="请选择部门状态" style="width:150px;">
          <el-option v-for="item in deptStatus" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchDept.managerId" @change="loadDept" placeholder="请选择部门负责人" style="width:180px;">
          <el-option v-for="item in deptArr" :label="item.managerName" :value="item.managerId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="loadDept">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:5px;">
    <div style="margin:10px 0px 40px 10px;">
      部门列表
      <el-button type="primary" style="float: right;margin-top:-3px;" @click="addDept">
        新增部门
      </el-button>
    </div>
    <el-table :data="deptArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="name" label="部门名称" align="center"/>
      <el-table-column prop="managerName" label="负责人" align="center"/>
      <el-table-column prop="phone" label="联系方式" align="center"/>
      <el-table-column prop="location" label="部门位置" align="center"/>
      <el-table-column prop="description" label="部门描述" align="center"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column prop="updateTime" label="更新时间" align="center"/>
      <el-table-column prop="status" label="部门状态" align="center" :formatter="deptStatusFormatter"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editDept(scope.row)">编辑</el-button>
          <el-button link type="primary" size="small" @click="delDept(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <el-dialog :title="dialogTitle" v-model="dialogVisible"
             :close-on-click-modal="false" style="width:800px;padding:40px;">
    <el-form label-position="top" label-width="80px">
      <el-form-item label="部门名称">
        <el-input v-model="dept.name" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-input v-model="dept.managerName" @focusout="searchManager" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="联系方式">
        <el-input v-model="dept.phone" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="部门位置">
        <el-input v-model="dept.location" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="部门描述">
        <el-input v-model="dept.description" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="部门状态">
        <el-select v-model="dept.status" placeholder="请选择">
          <el-option v-for="item in deptStatus" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button @click="saveDept" :disabled="!disable" type="primary">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";

const searchDept=ref({})
const deptArr=ref([])
const deptStatus=ref([])
const dialogVisible=ref(false)
const dialogTitle=ref("")
const disable=ref(false)
const reset = () => {
  searchDept.value={}
  loadDept()
}
const loadDept = () => {
  axios.post(BASE_URL+'/v1/department/select',searchDept.value)
      .then((response)=>{
        if (response.data.code==2000){
          deptArr.value=response.data.data
          searchDept.value={}
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}
onMounted(()=> {
  loadDept();
  loadDictOptions(deptStatus,"DeptStatus")
})
const deptStatusFormatter=(row,column,cellValue,index)=>{
  if (!deptStatus){
    return cellValue;
  }
  for (let item of deptStatus.value){
    if (item.value == cellValue){
      cellValue = item.label
    }
  }
  return cellValue;
}
const addDept = () =>{
  dialogVisible.value=true
  dialogTitle.value="新建部门"
  dept.value={}
  disable.value=false
}
const dept=ref({})
const saveDept = () => {
  if (confirm("确认保存吗")){
    axios.post(BASE_URL+'/v1/department/save',dept.value)
        .then((response)=>{
          if (response.data.code){
            ElMessage.success("保存成功")
            loadDept()
            dialogVisible.value=false
          }else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}
const handleClose = () => {
  if (confirm("确定要关闭窗口吗")){
    dialogVisible.value=false
  }
}
const editDept = (row) => {
  loadDept()
  dept.value=row
  dialogVisible.value=true
  dialogTitle.value="编辑部门信息"
  disable.value=false
}
const delDept = (id) => {
  if (confirm("确定要删除该部门吗，删除后关联该部门的员工部门信息会被清空")){
    axios.delete(BASE_URL+'/v1/department/deleteById/'+id)
        .then((response)=>{
          if (response.data.code==2000){
            ElMessage.success("删除成功")
            loadDept()
          }else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}
const searchManager = () => {
  axios.get(BASE_URL+'/v1/employee/select/'+dept.value.managerName)
      .then((response)=>{
        if (response.data.code==2000){
          dept.value.managerId=response.data.data.id
          disable.value=true
        }else {
          ElMessage.error("无此员工")
        }
      })
}
</script>

<style scoped>

</style>